const state = {
  isLoggedIn: !!uni.getStorageSync('isLoggedIn'),
    userInfo: uni.getStorageSync('user') || null
}

const mutations = {
  SET_LOGIN_STATUS(state, status) {
    state.isLoggedIn = status
    // 持久化登录状态
    if (status) {
      uni.setStorageSync('isLoggedIn', true)
    } else {
      uni.removeStorageSync('isLoggedIn')
    }
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
    // 持久化用户信息
    if (userInfo) {
      uni.setStorageSync('user', userInfo)
    } else {
      uni.removeStorageSync('user')
    }
  },
  CLEAR_USER(state) {
    state.isLoggedIn = false
    state.userInfo = null
    // 清除持久化数据
    uni.removeStorageSync('isLoggedIn')
    uni.removeStorageSync('user')
    uni.removeStorageSync('token')
  }
}

const actions = {
  setLoginStatus({ commit }, status) {
    commit('SET_LOGIN_STATUS', status)
  },
  setUserInfo({ commit }, userInfo) {
    commit('SET_USER_INFO', userInfo)
  },
  logout({ commit }) {
    commit('CLEAR_USER')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}